package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于插件的
 */
@Configuration
public class DelayMessageConfig {

    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    //交换机
    //direct: 定向交换机 路由模型
    //fanout: 扇出交换机 订阅模型
    //topic:  主题交换机 主题模型
    //但是基于插件的 x-delayed-message 需要使用自定义交换机
    @Bean
    public Exchange delayExchange() {
        Map<String, Object> arguments = new HashMap<>();
        //自定义交换机类型x-delayed-type
        arguments.put("x-delayed-type", "direct");//虽然基于插件 但还是direct类型 因为需要绑定队列
        //自定义交换机中延迟消息类型x-delayed-message
        return new CustomExchange(exchange_delay, "x-delayed-message", true, false, arguments);
    }

    //普通队列
    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable(queue_delay_1).build();
    }

    @Bean
    public Binding bindingDelayQueueToDelayExchange(
            //delayExchange是IOC容器中交换机的名字 delayQueue同理
            @Qualifier(value = "delayExchange") Exchange delayExchange,
            @Qualifier(value = "delayQueue") Queue delayQueue) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(routing_delay).noargs();
    }

}
